1. Field of the Invention
The invention relates in general to an apparatus and method for initializing a buffer, and more particularly to a stochastic initialization apparatus and method for an elastic buffer.
2. Description of the Related Art
In an electrical device, many units have to transmit data. FIG. 1 is a block diagram showing an electrical device. The electrical device 100 includes a first unit 110, a second unit 130, and a buffer 120. The first unit 110 is for outputting data to the second unit 130 averagely at the same speed as the second unit 130 for receiving the data over a long period of time. But, over a short period of time, the speed that the first unit 110 outputs and the speed that the second unit 130 inputs is different. Thus, the buffer 120 is needed for temporary data storage.
The buffer 120 may be caused the buffer underrun error and buffer overrun error. The buffer underrun error means that the buffer region of the buffer 120 is empty, and no data can be read. If the reading speed is higher than the writing speed, the buffer underrun error may occur. The buffer overrun error means that the buffer region of the buffer 120 is full and no data can be further written. If the writing speed is higher than the reading speed, the buffer overrun error may occur.
FIG. 2 is a schematic illustration showing the buffer 120. The buffer 120 is a FIFO (First In First Out) buffer having storage zones that may be regarded as being disposed in a ring. In this example, the buffer has 11 storage zones receptively represented by 0 to 10. The buffer 120 uses the writing index W to indicate the position of the buffer 120 into which the first unit 110 writes, and the reading index R to indicate the position of the buffer 120 from which the second unit 130 reads. In FIG. 2, the reading index R directs to the storage zone 1, which means that the reading process starts at the storage zone 1, and the reserved reading zones are the storage zones 1 to 7. The writing index W is at the position of the storage zone 8, and the reserved writing zones thereof are the storage zones 8 to 10, which means that the writing process starts at the storage zone 8. The written data is filled into the corresponding storage zone according to the position of the writing index W. After a storage zone has been written, the writing index W advances a step clockwise. In the reading process, the data in the corresponding storage zone is read according to the position of the reading index R. Then, the reading index R advances a step clockwise. If the reading speed is too high such that the reading index R catches up with the writing index W, it means that the data stored in the buffer 120 is not enough for reading, and the buffer underrun error occurs in the buffer 120. If the writing speed is too high such that the writing index W catches up with the reading index R, it means that the buffer 120 has no space for writing, and the buffer overrun error occurs in the buffer 120.
In order to avoid the previous error of the buffer 120, the buffer 120 has to be initialized before data transmission. That is, when the buffer 120 is empty, the number of the reserved writing zones and the number of the reserved reading zones are determined in advance. In general, the numbers of the reserved writing zones and the reserved reading zones are set to be the same such that the buffer underrun or overrun error seldom occurs. However, if the number of the storage zones is an odd number, the initialization cannot just make the numbers of the reserved writing zones and the reserved reading zones the same such that the elastic buffer 120 tends to be caused the buffer underrun or overrun error.
FIG. 3A is a schematic illustration showing the buffer 120 that tends to cause the buffer underrun error. During the initialization, six storage zones after the writing index W are reserved for writing. That is, the number of the reserved writing zones is 6. In addition, five storage zones after the reading index are reserved for reading. That is, the number of the reserved reading zones is 5. The number of the reserved writing zones is larger than that of the reserved reading zones, so the buffer 120 in this case tends to be caused the buffer underrun error.
FIG. 3B is a schematic illustration showing the buffer that tends to cause the buffer overrun error. During the initialization, the number of the reserved writing zones is 5, and the number of the reserved reading zones is 6. The number of the reserved writing zones is smaller than that of the reserved reading zones, so the buffer in this case tends to cause the buffer overrun error.
Because the reading speed and the writing speed are not fixed and cannot be predicted, it is important subject to initialize the buffer in order to avoid the problem of buffer underrun or overrun error.